Sql Server Tables,Procedures:

1)Create Table:

CREATE TABLE [dbo].[tblStudent](
	[RollNo] [int] NOT NULL,
	[Name] [varchar](50) NULL,
	[Department] [varchar](50) NULL,
	[Total] [float] NULL,
	[Result] [varchar](10) NULL,
	[bFlage] [bit] NULL
) ON [PRIMARY]

2)Create Procedure for Inser ,Update, Delete

Ceate procedure [dbo].[SP_Student_Insert](@Mode varchar(5),@RollNo int,@Name varchar(50),@Department varchar(50),@Total float,@Result varchar(10))
  as
  if(@Mode='INS')
  begin
  insert into tblStudent (RollNo,Name,Department,Total,Result,bFlage)values(@RollNo,@Name,@Department,@Total,@Result,1)
  end
  if(@Mode='UPD')
  begin
  update tblStudent set Name=@Name,Department=@Department,Total=@Total,Result=@Result,bFlage=1 where RollNo=@RollNo
  end
  if(@Mode='DEL')
  begin
  update tblStudent set Name=@Name,Department=@Department,Total=@Total,Result=@Result,bFlage=0 where RollNo=@RollNo
  end
3)Create procedure for Select  Table values to bind Gridview

Create procedure [dbo].[SP_Stu_Select_GridData]
as
begin
select * from tblStudent where bFlage=1
end



Step 1:
---> First create the WCF Service 


IService.cs:
-----------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;

namespace WcfService1
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in Web.config.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
        [OperationContract]
        int InsertDate(string Mode,int RollNo, string Name, string Department, float Total, string Result);
        [OperationContract]
        DataSet GridData();
        

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}


Service.svc.cs:
--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace WcfService1
{
    // NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
    public class Service1 : IService1
    {
        SqlConnection con = new SqlConnection("Data Source=System 10;Initial Catalog=Test;User ID=sa;Password=Test1234");
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
        public int InsertDate(string Mode,int RollNo, string Name, string Department, float Total, string Result)
        {
            try
            {
                con.Open();
                SqlCommand com = new SqlCommand("SP_Student_Insert", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@Mode", Mode);
                com.Parameters.AddWithValue("@RollNo", RollNo);
                com.Parameters.AddWithValue("@Name", Name);
                com.Parameters.AddWithValue("@Department", Department);
                com.Parameters.AddWithValue("@Total", Total);
                com.Parameters.AddWithValue("@Result", Result);
                com.ExecuteNonQuery();
                con.Close();
                return 1;
            }
            catch
            {
                return 0;
            }
            
        }
        public DataSet GridData()
        {
            con.Open();
            DataSet l_dsGridData = new DataSet();
            SqlCommand com = new SqlCommand("SP_Stu_Select_GridData", con);
            com.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter l_da = new SqlDataAdapter(com);
            l_da.Fill(l_dsGridData);
            con.Close();
            return l_dsGridData;
            
        }
        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}


WpfApplication3.XAML:

<Window x:Class="WpfApplication3.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="500" Width="500" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit">
    
    <Grid Height="500" Width="500">

        <Label Height="28" HorizontalAlignment="Left" Margin="84,39,0,0" Name="label1" VerticalAlignment="Top" Width="120">RollNo :</Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="84,80,0,0" Name="label2" VerticalAlignment="Top" Width="120">Name    :</Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="84,117,0,0" Name="label3" VerticalAlignment="Top" Width="120">Department :</Label>
        <Label Height="28" HorizontalAlignment="Left" Margin="84,157,0,0" Name="label4" VerticalAlignment="Top" Width="120">Total  :</Label>
        <Label HorizontalAlignment="Left" Margin="84,196,0,0" Name="label5" Width="120" Height="28" VerticalAlignment="Top">Result  :</Label>
        <TextBox Height="23" Margin="250,39,130,0" Name="textBox1" VerticalAlignment="Top" />
        <TextBox Height="23" Margin="250,79,130,0" Name="textBox2" VerticalAlignment="Top" />
        <TextBox Height="23" Margin="250,117,130,0" Name="textBox3" VerticalAlignment="Top" />
        <TextBox Height="23" Margin="250,157,130,0" Name="textBox4" VerticalAlignment="Top" />
        <TextBox Margin="0,196,130,0" Name="textBox5" Height="23" VerticalAlignment="Top" HorizontalAlignment="Right" Width="120" />
        <Button HorizontalAlignment="Left" Margin="94,242,0,235" Name="btnInsert" Width="75" Click="btnInsert_Click">Insert</Button>
        <Button Margin="193,242,0,235" Name="btnUpdate" Click="btnUpdate_Click" HorizontalAlignment="Left" Width="75">Update</Button>
        <Button HorizontalAlignment="Right" Margin="0,242,136,235" Name="btnDelete" Width="75" Click="btnDelete_Click">Delete</Button>
        <my:DataGrid Margin="0,0,12,12" AutoGenerateColumns="False" Name="GridData" Height="200" VerticalAlignment="Bottom" ItemsSource="{Binding}" SelectionChanged="GridData_SelectionChanged" RowEditEnding="GridData_RowEditEnding" CanUserAddRows="False">
            
            <my:DataGrid.Columns>
                
                <my:DataGridTextColumn Header="RollNo" Width="*" Binding="{Binding RollNo}"></my:DataGridTextColumn>
                <my:DataGridTextColumn Header="Name" Width="*" Binding="{Binding Name}"></my:DataGridTextColumn>
                <my:DataGridTextColumn Header="Department" Width="*" Binding="{Binding Department}"></my:DataGridTextColumn>
                <my:DataGridTextColumn Header="Total"  Width="*" Binding="{Binding Total}"></my:DataGridTextColumn>
                <my:DataGridTextColumn Header="Result" Width="*" Binding="{Binding Result}"></my:DataGridTextColumn>
                <my:DataGridTemplateColumn Header="Edit" Width="*">
                    <my:DataGridTemplateColumn.CellTemplate >
                        <DataTemplate>
                            <Button Click="btnUpdate_Click" >Edit</Button>
                        </DataTemplate>
                    </my:DataGridTemplateColumn.CellTemplate>
                </my:DataGridTemplateColumn>
            </my:DataGrid.Columns>

        </my:DataGrid>
    </Grid>
</Window>

wpfApplication3.cs:
-------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;


namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        ServiceReference1.Service1Client cc = new ServiceReference1.Service1Client();
        public Window1()
        {
            InitializeComponent();

            ShowData();
        }
        public void ShowData()
        {
            DataSet ds = new DataSet();
            ds = cc.GridData();
            GridData.ItemsSource = ds.Tables[0].DefaultView;
        }
        public string Name, Department, Result;
        public int RollNo;
        public int l_intResult=0;
        public float Total;
        private void btnInsert_Click(object sender, RoutedEventArgs e)
      {
          try
          {
              RollNo = Convert.ToInt16(textBox1.Text);
              Name = textBox2.Text;
              Department = textBox3.Text;
              Total = Convert.ToSingle(textBox4.Text);
              Result = textBox5.Text;

              l_intResult = cc.InsertDate("INS", RollNo, Name, Department, Total, Result);
              if (l_intResult > 0)
              {
                  clear();
                  ShowData();
                  MessageBox.Show("Values are inserted");
              }
          }
          catch
          {
          }
          
      }

        private void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RollNo = Convert.ToInt16(textBox1.Text);
                Name = textBox2.Text;
                Department = textBox3.Text;
                Total = Convert.ToSingle(textBox4.Text);
                Result = textBox5.Text;

                l_intResult = cc.InsertDate("UPD", RollNo, Name, Department, Total, Result);
                if (l_intResult > 0)
                {
                    clear();
                    ShowData();
                    MessageBox.Show("Values are Updated");
                }
            }
            catch
            {
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                RollNo = Convert.ToInt16(textBox1.Text);
                Name = textBox2.Text;
                Department = textBox3.Text;
                Total = Convert.ToSingle(textBox4.Text);
                Result = textBox5.Text;

                l_intResult = cc.InsertDate("DEL", RollNo, Name, Department, Total, Result);
                if (l_intResult > 0)
                {
                    clear();
                    ShowData();
                    MessageBox.Show("Values are Deleted");
                }
            }
            catch
            {
            }
        }

        private void GridData_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                object Item = GridData.SelectedItem;
                int l_intRollNo = Convert.ToInt16((GridData.SelectedCells[0].Column.GetCellContent(Item) as TextBlock).Text);
                string l_strName = (GridData.SelectedCells[1].Column.GetCellContent(Item) as TextBlock).Text;
                string l_strDepatment = (GridData.SelectedCells[2].Column.GetCellContent(Item) as TextBlock).Text;
                float l_floTotal = Convert.ToSingle((GridData.SelectedCells[3].Column.GetCellContent(Item) as TextBlock).Text);
                string l_strResult = (GridData.SelectedCells[4].Column.GetCellContent(Item) as TextBlock).Text;
                textBox1.Text = l_intRollNo.ToString();
                textBox2.Text = l_strName.ToString();
                textBox3.Text = l_strDepatment.ToString();
                textBox4.Text = l_floTotal.ToString();
                textBox5.Text = l_strResult.ToString();
                ShowData();
            }
            catch
            {
            }
            
          
        }

        private void GridData_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
        {
          
        }
        void clear()
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
        }
    }

}

